﻿using DevExpress.XtraEditors;
using SMALLERP.ComClass;
using SMALLERP.DataClass;
using SMALLERP.PM;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using XJLSC.QM;

namespace XJLSC.INV
{
    public partial class CPGYLXGL : Form
    {
      
        DataBase db = new DataBase();
        private SqlDataReader sdr1;
        SCKC f;
        DDKC g;
        public CPGYLXGL()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string strSql = "";
                if(checkBox1.CheckState==CheckState.Unchecked)
                {
                    strSql = "SELECT A.UDF021 LX,B.ITEM_CODE PH,B.ITEM_NAME 品名,B.ITEM_SPECIFICATION 规格,A.ROUTING_CODE 工艺路线编号,B.DRAWING_NO 图号 FROM ITEM_ROUTING A LEFT JOIN ITEM B ON A.ITEM_ID=B.ITEM_BUSINESS_ID WHERE B.ITEM_CODE LIKE '%" + textBox1.Text + "%'";
                }
                else
                {
                    strSql = "SELECT '无工艺路线' LX,B.ITEM_CODE PH,B.ITEM_NAME 品名,B.ITEM_SPECIFICATION 规格,'' 工艺路线编号,B.DRAWING_NO FROM ITEM_PLANT LEFT JOIN ITEM B ON B.ITEM_BUSINESS_ID=ITEM_PLANT.ITEM_ID LEFT JOIN PLANT ON PLANT_ID=ITEM_PLANT.Owner_Org_ROid WHERE ISNULL(B.ITEM_CODE,'') LIKE 'A%' AND NOT EXISTS(SELECT * FROM ITEM_ROUTING A WHERE B.ITEM_BUSINESS_ID=A.ITEM_ID) AND EXISTS(SELECT * FROM ITEM_PLANT C WHERE B.ITEM_BUSINESS_ID=C.ITEM_ID AND C.ITEM_PROPERTY='M') AND B.[STATUS]='2' AND ITEM_NAME NOT LIKE '%停用%' AND EXISTS(SELECT * FROM SALES_ORDER_DOC_SD LEFT JOIN SALES_ORDER_DOC_D ON SALES_ORDER_DOC_SD.SALES_ORDER_DOC_D_ID=SALES_ORDER_DOC_D.SALES_ORDER_DOC_D_ID WHERE SALES_ORDER_DOC_SD.[CLOSE]='0' AND SALES_ORDER_DOC_D.ITEM_ID=B.ITEM_BUSINESS_ID)  AND ITEM_PLANT.ApproveStatus='Y'";
                }
                //textBox1.Text =strSql;
                DataTable dt;
                db = new DataBase();
                dt = db.GetDataSet1(strSql, "MOCTB").Tables["MOCTB"];
                if (checkBox1.CheckState == CheckState.Unchecked)
                {
                    DataRow dr = dt.NewRow();
                    dt.Rows.Add(dr);
                    dr[0] = "";
                    dr[1] = "压机组装";
                    dr[2] = "压机组装";
                    dr[3] = "";
                    dr[4] = "";
                    dr[5] = "";

                    DataRow dr1 = dt.NewRow();
                    dt.Rows.Add(dr1);
                    dr1[0] = "";
                    dr1[1] = "压机加工";
                    dr1[2] = "压机加工";
                    dr1[3] = "";
                    dr1[4] = "";
                    dr1[5] = "";

                    DataRow dr2 = dt.NewRow();
                    dt.Rows.Add(dr2);
                    dr2[0] = "";
                    dr2[1] = "氢碎组装";
                    dr2[2] = "氢碎组装";
                    dr2[3] = "";
                    dr2[4] = "";
                    dr2[5] = "";

                    DataRow dr3 = dt.NewRow();
                    dt.Rows.Add(dr3);
                    dr3[0] = "";
                    dr3[1] = "氢碎加工";
                    dr3[2] = "氢碎加工";
                    dr3[3] = "";
                    dr3[4] = "";
                    dr3[5] = "";

                    DataRow dr4 = dt.NewRow();
                    dt.Rows.Add(dr4);
                    dr4[0] = "";
                    dr4[1] = "码盘组装";
                    dr4[2] = "码盘组装";
                    dr4[3] = "";
                    dr4[4] = "";
                    dr4[5] = "";

                    DataRow dr5 = dt.NewRow();
                    dt.Rows.Add(dr5);
                    dr5[0] = "";
                    dr5[1] = "码盘加工";
                    dr5[2] = "码盘加工";
                    dr5[3] = "";
                    dr5[4] = "";
                    dr5[5] = "";

                    DataRow dr6 = dt.NewRow();
                    dt.Rows.Add(dr6);
                    dr6[0] = "";
                    dr6[1] = "混料组装";
                    dr6[2] = "混料组装";
                    dr6[3] = "";
                    dr6[4] = "";
                    dr6[5] = "";

                    DataRow dr7 = dt.NewRow();
                    dt.Rows.Add(dr7);
                    dr7[0] = "";
                    dr7[1] = "混料加工";
                    dr7[2] = "混料加工";
                    dr7[3] = "";
                    dr7[4] = "";
                    dr6[5] = "";
                }
                gridControl2.DataSource = dt;
                treeList1.DataSource = dt;
                treeList1.KeyFieldName = "PH";
                treeList1.ParentFieldName = "LX";
                //treeList1.ExpandAll();
                treeList1.Refresh();
            }
            catch
            {

            }
        }

        private void gridView2_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {
            try
            {
                gridControl1.DataSource = null;
                string strSql = "";
                String gyph = this.gridView2.GetFocusedRowCellValue("PH").ToString();
                String gybh = this.gridView2.GetFocusedRowCellValue("工艺路线编号").ToString();
                strSql = "SELECT OPERATION_SEQ 工序号,B.OPERATION_CODE 工艺编号,B.OPERATION_NAME 工艺名称,A.OPERATION_COMMENT 工艺说明,A.FIXED_MAN_HOUR 固定工时,A.DYNAMIC_MAN_HOUR 变动工时,CAST(A.PIECE_PRICE AS real) 工价,A.REMARK 上阶品号 FROM ITEM_ROUTING_D A LEFT JOIN OPERATION B ON A.OPERATION_ID=B.OPERATION_ID LEFT JOIN ITEM_ROUTING C ON A.ITEM_ROUTING_ID=C.ITEM_ROUTING_ID LEFT JOIN ITEM D ON D.ITEM_BUSINESS_ID=C.ITEM_ID WHERE D.ITEM_CODE='" + gyph + "' AND C.ROUTING_CODE='" + gybh + "'";
                DataTable dt;
                db = new DataBase();
                dt = db.GetDataSet1(strSql, "MOCTB").Tables["MOCTB"];
                gridControl1.DataSource = dt;
            }
            catch
            {

            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.Title = "导出Excel";
            fileDialog.Filter = "Excel文件(*.xls)|*.xls";
            DialogResult dialogResult = fileDialog.ShowDialog(this);
            if (dialogResult == DialogResult.OK)
            {
                DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
                gridControl2.ExportToXls(fileDialog.FileName);
                DevExpress.XtraEditors.XtraMessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.Title = "导出Excel";
            fileDialog.Filter = "Excel文件(*.xls)|*.xls";
            DialogResult dialogResult = fileDialog.ShowDialog(this);
            if (dialogResult == DialogResult.OK)
            {
                DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
                gridControl1.ExportToXls(fileDialog.FileName);
                DevExpress.XtraEditors.XtraMessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if(PropertyClass.jiaose != "工艺维护")
            {
                MessageBox.Show("您无权删除工艺路线","系统提示");
                return;
            }

            try
            {
                String gyph = this.gridView2.GetFocusedRowCellValue("PH").ToString();
                String gybh = this.gridView2.GetFocusedRowCellValue("工艺路线编号").ToString();
                string strSql1 = "DELETE FROM ITEM_ROUTING_D WHERE EXISTS(SELECT * FROM (SELECT A.ITEM_ROUTING_ID FROM ITEM_ROUTING A LEFT JOIN ITEM B ON A.ITEM_ID=B.ITEM_BUSINESS_ID WHERE B.ITEM_CODE='" + gyph + "' AND A.ROUTING_CODE='" + gybh + "') Z WHERE Z.ITEM_ROUTING_ID=ITEM_ROUTING_D.ITEM_ROUTING_ID)";
               
                try
                {
                    if (db.ExecDataBySql1(strSql1) > 0)
                    {
                    }
                    else
                    {
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    throw ex;
                }
            }
            catch
            {

            }

            try
            {
                String gyph = this.gridView2.GetFocusedRowCellValue("PH").ToString();
                String gybh = this.gridView2.GetFocusedRowCellValue("工艺路线编号").ToString();
                string strSql1 = "DELETE FROM ITEM_ROUTING WHERE EXISTS(SELECT * FROM (SELECT A.ITEM_ROUTING_ID FROM ITEM_ROUTING A LEFT JOIN ITEM B ON A.ITEM_ID=B.ITEM_BUSINESS_ID WHERE B.ITEM_CODE='" + gyph + "' AND A.ROUTING_CODE='" + gybh + "') Z WHERE Z.ITEM_ROUTING_ID=ITEM_ROUTING.ITEM_ROUTING_ID)";
                try
                {
                    if (db.ExecDataBySql1(strSql1) > 0)
                    {
                    }
                    else
                    {
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    throw ex;
                }
            }
            catch
            {

            }
            MessageBox.Show("删除成功！","系统提示");
            button1.PerformClick();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "Excel文件";
            ofd.FileName = "";
            ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            ofd.Filter = "所有文件(*.*)|*.*|Excel2003文件(*.xls)|*.xls|Excel2007文件(*.xlsx)|*.xlsx";
            ofd.ValidateNames = true;
            ofd.CheckFileExists = true;
            ofd.CheckPathExists = true;
            string strName = string.Empty;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                strName = ofd.FileName;
            }
            if (strName == "")
            {
                XtraMessageBox.Show("没有选择Excel文件！无法进行数据导入");
                return;
            }
            else
            {
                try
                {
                    DataSet myDs = new DataSet();
                    string text = string.Format("Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = '{0}';Extended Properties=Excel 8.0", strName);
                    string excelFirstTableName = GetExcelFirstTableName(strName);
                    myDs.Tables.Clear();
                    myDs.Clear();
                    this.gridControl3.DataSource = null;
                    OleDbConnection selectConnection = new OleDbConnection(text);
                    OleDbDataAdapter oleDbDataAdapter = new OleDbDataAdapter(string.Format("select * from [{0}]", excelFirstTableName), selectConnection);
                    oleDbDataAdapter.Fill(myDs);
                    this.gridControl3.DataSource = myDs.Tables[0];
                    this.gridView3.PopulateColumns();
                }
                catch (Exception ex)
                {
                    XtraMessageBox.Show("从电子表格文件中装载数据异常！", ex.Message);
                }
            }
        }

        public static string GetExcelFirstTableName(string excelFileName)
        {
            string tableName = null;
            if (File.Exists(excelFileName))
            {
                using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet." +
                  "OLEDB.4.0;Extended Properties=\"Excel 8.0\";Data Source=" + excelFileName))
                {
                    conn.Open();
                    DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                    tableName = dt.Rows[0][2].ToString().Trim();
                }
            }
            return tableName;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            //try
            //{
                string strSql = "";
                int[] rows = this.gridView3.GetSelectedRows();
                db = new DataBase();
                DataTable dta = new DataTable();
               dta.Columns.Add("阶次");
               dta.Columns.Add("主件品号");
               dta.Columns.Add("主件品名");
               dta.Columns.Add("主件规格");
               dta.Columns.Add("元件品号");
               dta.Columns.Add("元件品名");
               dta.Columns.Add("元件规格");
               dta.Columns.Add("图号");
               dta.Columns.Add("组成用量");
               dta.Columns.Add("底数");
               dta.Columns.Add("工艺");

            foreach (int row in rows)
                {
                    String mo_ph = this.gridView3.GetRowCellValue(row, "品号").ToString();
                    strSql = "SELECT Path 阶次,R_ZJPH 主件品号,A.ITEM_NAME 主件品名,A.ITEM_SPECIFICATION 主件规格,R_YJPH 元件品号,B.ITEM_NAME 元件品名,B.ITEM_SPECIFICATION 元件规格,RTRIM(B.DRAWING_NO) 图号,CAST(R_QTY_PER AS REAL) 组成用量,R_DENOMINATOR 底数,ISNULL(C.OPERATION_NAME,'') 工艺 FROM d_Bom('" + mo_ph + "') Z LEFT JOIN ITEM A ON R_ZJPH=A.ITEM_CODE LEFT JOIN ITEM B ON B.ITEM_CODE=R_YJPH LEFT JOIN OPERATION C ON C.OPERATION_ID=Z.R_OPERATION_ID ORDER BY [Path]";
                    //textBox1.Text =strSql;
                    DataTable dt;

                    dt = db.GetDataSet1(strSql, "MOCTB").Tables["MOCTB"];
                    for (int i = 0; i <= dt.Rows.Count - 1; i++)
                    {
                         DataRow dr = dta.NewRow();
                        dr["阶次"] = dt.Rows[i][0].ToString();
                        dr["主件品号"] = dt.Rows[i][1].ToString();
                        dr["主件品名"] = dt.Rows[i][2].ToString();
                        dr["主件规格"] = dt.Rows[i][3].ToString();
                        dr["元件品号"] = dt.Rows[i][4].ToString();
                        dr["元件品名"] = dt.Rows[i][5].ToString();
                        dr["元件规格"] = dt.Rows[i][6].ToString();
                        dr["图号"] = dt.Rows[i][7].ToString();
                        dr["组成用量"] = dt.Rows[i][8].ToString();
                        dr["底数"] = dt.Rows[i][9].ToString();
                        dr["工艺"] = dt.Rows[i][10].ToString();
                    //this.gridView4.SetFocusedRowCellValue("阶次", dt.Rows[i][0].ToString());
                    //this.gridView4.SetFocusedRowCellValue("主件品号", dt.Rows[i][1].ToString());
                    //this.gridView4.SetFocusedRowCellValue("主件品名", dt.Rows[i][2].ToString());
                    //this.gridView4.SetFocusedRowCellValue("主件规格", dt.Rows[i][3].ToString());
                    //this.gridView4.SetFocusedRowCellValue("元件品号", dt.Rows[i][4].ToString());
                    //this.gridView4.SetFocusedRowCellValue("元件品名", dt.Rows[i][5].ToString());
                    //this.gridView4.SetFocusedRowCellValue("元件规格", dt.Rows[i][6].ToString());
                    //this.gridView4.SetFocusedRowCellValue("组成用量", dt.Rows[i][7].ToString());
                    //this.gridView4.SetFocusedRowCellValue("底数", dt.Rows[i][8].ToString());
                    //this.gridView4.SetFocusedRowCellValue("工艺", dt.Rows[i][9].ToString());
                    dta.Rows.Add(dr);

                }
                }
            gridControl4.DataSource = dta;
            gridControl4.MainView.PopulateColumns();
            //}
            //catch
            //{

            //}
        }

        private void button7_Click(object sender, EventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)
        {
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.Title = "导出Excel";
            fileDialog.Filter = "Excel文件(*.xls)|*.xls";
            DialogResult dialogResult = fileDialog.ShowDialog(this);
            if (dialogResult == DialogResult.OK)
            {
                DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
                gridControl4.ExportToXls(fileDialog.FileName);
                DevExpress.XtraEditors.XtraMessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void button7_Click_1(object sender, EventArgs e)
        {
            int[] rows = this.gridView2.GetSelectedRows();
            int i = 0;
            foreach (int row in rows)
            {
                String mo_ph = this.gridView2.GetRowCellValue(row, "PH").ToString();
                String mo_lx = this.gridView2.GetRowCellValue(row, "LX").ToString();
                string strSql1 = "UPDATE ITEM_ROUTING SET UDF021='" + mo_lx + "' WHERE EXISTS(SELECT * FROM ITEM WHERE ITEM.ITEM_BUSINESS_ID=ITEM_ROUTING.ITEM_ID AND ITEM.ITEM_CODE='" + mo_ph + "')";
             
                try
                {
                    if (db.ExecDataBySql1(strSql1) > 0)
                    {
                        i++;
                    }
                    else
                    {
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    throw ex;
                }
            }
            if(i>0)
            {
                MessageBox.Show("更新成功" + i + "笔","软件提示");
            }
            else
            {
                MessageBox.Show("没有更新成功！","软件提示");
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            String mo_ph = this.gridView2.GetFocusedRowCellValue("PH").ToString();
            String mo_bh = this.gridView2.GetFocusedRowCellValue("工艺路线编号").ToString();
            int[] rows = this.gridView1.GetSelectedRows();
            int i = 0;
            foreach (int row in rows)
            {
                String mo_gxh = this.gridView1.GetRowCellValue(row, "工序号").ToString();
                String mo_sjph = this.gridView1.GetRowCellValue(row, "上阶品号").ToString();
                string strSql1 = "UPDATE ITEM_ROUTING_D SET REMARK='" + mo_sjph + "' WHERE EXISTS(SELECT * FROM ITEM_ROUTING LEFT JOIN ITEM ON ITEM_ROUTING.ITEM_ID=ITEM.ITEM_BUSINESS_ID WHERE ITEM_ROUTING_D.ITEM_ROUTING_ID=ITEM_ROUTING.ITEM_ROUTING_ID AND ITEM.ITEM_CODE='" + mo_ph + "' AND ROUTING_CODE='" + mo_bh + "') AND OPERATION_SEQ='" + mo_gxh + "'";
                //textBox1.Text = strSql1;
                try
                {
                    if (db.ExecDataBySql1(strSql1) > 0)
                    {
                        i++;
                    }
                    else
                    {
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    throw ex;
                }
            }
            if (i > 0)
            {
                MessageBox.Show("更新成功" + i + "笔", "软件提示");
            }
            else
            {
                MessageBox.Show("没有更新成功！", "软件提示");
            }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            String strSql = "";
            strSql = "SELECT BOM_ID,RTRIM(ITEM.ITEM_CODE) 品号,RTRIM(ITEM.ITEM_NAME) 品名,RTRIM(ITEM.ITEM_SPECIFICATION) 规格,RTRIM(ITEM.DRAWING_NO) 图号 FROM BOM LEFT JOIN ITEM ON BOM.ITEM_ID=ITEM.ITEM_BUSINESS_ID WHERE ITEM_CODE LIKE '%" + textBox2.Text + "%' AND (ITEM_NAME LIKE '%" + textBox3.Text + "%' OR ITEM_SPECIFICATION LIKE '%" + textBox3.Text + "%' OR ITEM.DRAWING_NO LIKE '%" + textBox3.Text + "%')";
            try
            {
                DataTable dt;
                dt = db.GetDataSet1(strSql, "MOCTA").Tables["MOCTA"];
                gridControl5.DataSource = dt;
                //gridView1.ApplyFindFilter("[请购数量]>'0'");               
            }
            catch
            {

            }
        }

        private void gridView5_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {
            try
            {
                gridControl1.DataSource = null;
                string strSql = "";
                String bomid = this.gridView5.GetFocusedRowCellValue("BOM_ID").ToString();
                strSql = "SELECT BOM_D_ID,RTRIM(ITEM.ITEM_CODE) 品号,RTRIM(ITEM.ITEM_NAME) 品名,RTRIM(ITEM_SPECIFICATION) 规格,RTRIM(DRAWING_NO) 图号,CAST(QTY_PER AS REAL) 组成用量,CAST(DENOMINATOR AS REAL) 底数,ISNULL(OPERATION.OPERATION_CODE,'') 工艺,RTRIM(ISNULL(OPERATION.OPERATION_NAME,'')) 工艺名称,BOM_D.REMARK 备注 FROM BOM_D LEFT JOIN ITEM ON BOM_D.SOURCE_ID_ROid=ITEM.ITEM_BUSINESS_ID LEFT JOIN OPERATION ON BOM_D.OPERATION_ID=OPERATION.OPERATION_ID WHERE BOM_D.BOM_ID='" + bomid + "'";
                DataTable dt;
                db = new DataBase();
                dt = db.GetDataSet1(strSql, "MOCTB").Tables["MOCTB"];
                gridControl6.DataSource = dt;
            }
            catch
            {

            }
        }

        private void button11_Click(object sender, EventArgs e)
        {
            int[] rows = this.gridView6.GetSelectedRows();
            int i = 0;
            foreach (int row in rows)
            {
                String bom_d_id = this.gridView6.GetRowCellValue(row, "BOM_D_ID").ToString();
                string strSql1 = "UPDATE BOM_D SET OPERATION_ID=ISNULL((SELECT OPERATION_ID FROM OPERATION WHERE OPERATION_CODE='" + textBox4.Text + "'),'00000000-0000-0000-0000-000000000000'),UDF026='" + PropertyClass.OperatorName + "'+'于'+'" + DateTime.Now.ToString() + "'+'更改工艺' WHERE BOM_D_ID='" + bom_d_id + "'";
                //textBox1.Text = strSql1;
                try
                {
                    if (db.ExecDataBySql1(strSql1) > 0)
                    {
                        i++;
                    }
                    else
                    {
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    throw ex;
                }
            }
            if (i > 0)
            {
                MessageBox.Show("更新成功" + i + "笔", "软件提示");
            }
            else
            {
                MessageBox.Show("没有更新成功！", "软件提示");
            }
        }

        private void button12_Click(object sender, EventArgs e)
        {
            g = new DDKC();
            g.kclx = "gongyi";
            g.DataSelected += new EventHandler(g_DataSelected);
            //f.cdgzzx += new SCKC.Cdgzzx(textBox2_TextChanged);
            g.Show(this);
        }

        void g_DataSelected(object sender, EventArgs e)
        {
            textBox4.Text = g.UserId;
         
        }

        private void button14_Click(object sender, EventArgs e)
        {
            string strSql;
            if (checkBox3.CheckState == CheckState.Unchecked)
            {
                if (checkBox2.CheckState == CheckState.Unchecked)
                {
                    strSql = "SELECT DISTINCT ITEM.ITEM_CODE 品号,ITEM.ITEM_NAME 品名,ITEM.ITEM_SPECIFICATION 规格,ITEM_PROPERTY 品号属性 FROM ITEM LEFT JOIN ITEM_PLANT ON ITEM.ITEM_BUSINESS_ID=ITEM_PLANT.ITEM_ID WHERE EXISTS(SELECT * FROM MO WHERE MO.[STATUS]<>'Y' AND MO.[STATUS]<>'y' AND MO.ITEM_ID=ITEM.ITEM_BUSINESS_ID) AND ITEM_PROPERTY<>'P' AND NOT EXISTS(SELECT * FROM BOM_D WHERE BOM_D.PARENT_ITEM_ID=ITEM.ITEM_BUSINESS_ID)";
                }
                else
                {
                    strSql = "SELECT DISTINCT  DELIVERY_PLANT_ID 工厂ID,ITEM.ITEM_CODE 品号,ITEM.ITEM_NAME 品名,ITEM.ITEM_SPECIFICATION 规格,ITEM_PROPERTY 品号属性 FROM SALES_ORDER_DOC_SD LEFT JOIN SALES_ORDER_DOC_D ON SALES_ORDER_DOC_SD.SALES_ORDER_DOC_D_ID= SALES_ORDER_DOC_D.SALES_ORDER_DOC_D_ID LEFT JOIN SALES_ORDER_DOC ON SALES_ORDER_DOC_D.SALES_ORDER_DOC_ID=SALES_ORDER_DOC.SALES_ORDER_DOC_ID LEFT JOIN ITEM ON ITEM.ITEM_BUSINESS_ID=SALES_ORDER_DOC_D.ITEM_ID LEFT JOIN ITEM_PLANT ON DELIVERY_PLANT_ID=ITEM_PLANT.Owner_Org_ROid AND SALES_ORDER_DOC_D.ITEM_ID=ITEM_PLANT.ITEM_ID WHERE  ITEM_PROPERTY<>'P' AND NOT EXISTS(SELECT * FROM BOM_D WHERE BOM_D.PARENT_ITEM_ID=ITEM.ITEM_BUSINESS_ID)";
                }

                //textBox1.Text = strSql;
                try
                {
                    DataTable dt;
                    dt = db.GetDataSet1(strSql, "PURTD").Tables["PURTD"];
                    gridControl3.DataSource = dt;
                }
                catch
                {

                }
            }
            else
            {
                strSql = "SELECT BOM_D.SequenceNumber 阶次,A.ITEM_CODE 主件品号,A.ITEM_NAME 主件品名,A.ITEM_SPECIFICATION 主件规格,B.ITEM_CODE 元件品号,B.ITEM_NAME 元件品名,B.ITEM_SPECIFICATION 元件规格,CAST(QTY_PER AS REAL) 组成用量,CAST(DENOMINATOR AS REAL) 底数,ISNULL(OPERATION.OPERATION_NAME,'') 工艺 FROM BOM_D LEFT JOIN ITEM A ON A.ITEM_BUSINESS_ID=BOM_D.PARENT_ITEM_ID LEFT JOIN ITEM B ON B.ITEM_BUSINESS_ID=BOM_D.SOURCE_ID_ROid LEFT JOIN OPERATION ON OPERATION.OPERATION_ID=BOM_D.OPERATION_ID WHERE EXISTS(SELECT * FROM ITEM WHERE ITEM.ITEM_BUSINESS_ID=BOM_D.SOURCE_ID_ROid AND ITEM.ApproveStatus<>'Y') AND EXISTS(SELECT * FROM ITEM_PLANT C WHERE C.ITEM_ID=BOM_D.SOURCE_ID_ROid AND C.ITEM_PROPERTY<>'P') ORDER BY BOM_D.PARENT_ITEM_ID,BOM_D.SequenceNumber";
                try
                {
                    DataTable dt;
                    dt = db.GetDataSet1(strSql, "PURTD").Tables["PURTD"];
                    gridControl4.DataSource = dt;
                }
                catch
                {

                }
            }
        }

        private void button13_Click(object sender, EventArgs e)
        {
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.Title = "导出Excel";
            fileDialog.Filter = "Excel文件(*.xls)|*.xls";
            DialogResult dialogResult = fileDialog.ShowDialog(this);
            if (dialogResult == DialogResult.OK)
            {
                DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
                gridControl3.ExportToXls(fileDialog.FileName);
                DevExpress.XtraEditors.XtraMessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}
